package com.zhuangjie.rider.controller;

import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.enums.VerificationCodeScenes;
import com.zhuangjie.mvc.validator.annotate.PhoneNumber;
import com.zhuangjie.redis.helper.RedisGeoHelper;
import com.zhuangjie.rider.constant.RiderGeoInfoConstant;
import com.zhuangjie.rider.dto.RiderPasswordLoginDTO;
import com.zhuangjie.rider.dto.RiderPhoneCodeLoginDTO;
import com.zhuangjie.rider.dto.RiderSignUpDTO;
import com.zhuangjie.rider.feign.ThirdPartyFeignService;
import com.zhuangjie.rider.helper.RiderModuleTokenDataProvider;
import com.zhuangjie.rider.pojo.RiderBasicInfo;
import com.zhuangjie.rider.service.RiderService;
import com.zhuangjie.rider.vo.CommonInfosVO;
import com.zhuangjie.rider.vo.RiderWorkStatisticalDataVO;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.security.entity.AuthenticationTokens;
import com.zhuangjie.security.helper.JwtHelper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-12-26
 */
@RestController
@RequestMapping("/rider")
@Validated
public class RiderController {
    @Autowired
    private RiderService riderService;

    @Autowired
    private ThirdPartyFeignService thirdPartyFeignService;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private RedisGeoHelper redisGeoHelper;

    @GetMapping("/sendCodeForRiderRegister")
    @NoAuthentication
    public R sendCodeForRiderRegister(@NotBlank(message = "无效的手机号地区编号") String phoneAreaCode, @PhoneNumber(message = "无效的手机号") @RequestParam String phone) {
        return R.capture(()->thirdPartyFeignService.sendCode(VerificationCodeScenes.RIDER_REGISTER,phoneAreaCode,phone));
    }

    @GetMapping("/sendCodeForRiderLogin")
    @NoAuthentication
    public R sendCodeForRiderLogin(@NotBlank(message = "无效的手机号地区编号") @RequestParam String phoneAreaCode, @PhoneNumber(message = "无效的手机号") @RequestParam String phone) {
        return R.capture(()->thirdPartyFeignService.sendCode(VerificationCodeScenes.RIDER_LOGIN,phoneAreaCode,phone));
    }

    @PostMapping("/register")
    @NoAuthentication
    @ApiOperation("骑手注册")
    public R register(@Valid @RequestBody RiderSignUpDTO riderSignUpDTO) {
        return R.capture(()->riderService.riderRegister(riderSignUpDTO));
    }

    @PostMapping("/phoneCodeLogin")
    @NoAuthentication
    @ApiOperation("骑手手机验证码登录")
    public R riderPhoneCodeLogin(@Valid @RequestBody RiderPhoneCodeLoginDTO riderPhoneCodeLoginDTO) throws Exception {
        return R.ok().data(AuthenticationTokens.class,riderService.riderPhoneCodeLogin(riderPhoneCodeLoginDTO));
    }

    @PostMapping("/passwordLogin")
    @NoAuthentication
    @ApiOperation("骑手密码登录")
    public R riderPhoneCodeLogin(@Valid @RequestBody RiderPasswordLoginDTO riderPasswordLoginDTO) throws Exception {
        return R.ok().data(AuthenticationTokens.class,riderService.riderPasswordLogin(riderPasswordLoginDTO));
    }


    @GetMapping("/refreshToken")
    @NoAuthentication
    @ApiOperation("刷新token")
    public R refreshToken(@RequestParam String refreshToken) throws Exception {
        return R.require(AuthenticationTokens.class,riderService.refreshToken(refreshToken));
    }

    @GetMapping("/basic-info")
    @ApiOperation("获取用户基本信息")
    public R basicInfo()  {
        return R.require(RiderBasicInfo.class,riderService.getRiderBasicInfo());
    }

    @GetMapping("/common-infos")
    @ApiOperation("查询当前用户相关信息与配置信息")
    public R commonInfos() throws ExecutionException, InterruptedException {
        return R.require(CommonInfosVO.class,riderService.commonInfos());
    }

    @PostMapping("/change-work-status/{workStatus}")
    @ApiOperation("修改工作状态")
    public R  changeWorkStatus(@PathVariable("workStatus") Short workStatus)  {
        return R.capture(()->riderService.changeWorkStatus(workStatus));
    }

    @PostMapping("/remove-rider-geo")
    @ApiOperation("移除骑手位置信息")
    public R removeRiderGeo()  {
        return R.capture(()->redisGeoHelper.remove(RiderGeoInfoConstant.RIDER_GEO_INFO_KEY, String.valueOf(RiderModuleTokenDataProvider.getCurrentRiderId())));
    }
    @ApiOperation("骑手工作统计数据")
    @GetMapping("/rider-work-statistical-data")
    public R riderStatistic() {
        return  R.require(RiderWorkStatisticalDataVO.class,riderService.riderStatistic());
    }

}
